# Copyright 2009-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# This file is part of the gdb testsuite.

#
# This test tests some i386 general instructions for reverse execution.
#

if ![supports_reverse] {
    return
}


if ![istarget "i?86-*linux*"] then {
    verbose "Skipping i386 reverse tests."
    return
}

standard_testfile

# some targets have leading underscores on assembly symbols.
set additional_flags [gdb_target_symbol_prefix_flags]

if {[prepare_for_testing $testfile.exp $testfile $srcfile \
	 [list debug $additional_flags]]} {
    return -1
}

set end_of_main          [gdb_get_line_number " end of main "]
set end_of_inc_dec_tests [gdb_get_line_number " end inc_dec_tests "]

runto main

if [supports_process_record] {
    # Activate process record/replay
    gdb_test_no_output "record" "Turn on process record"
}

global hex
global decimal

gdb_test "step" "inc .eax.*" "step to inc eax 1st time"
send_gdb "info reg eax\n"
gdb_expect {
    -re "eax *($hex)\t.*$gdb_prompt " {
	set preinc_eax $expect_out(1,string)
    }
}

gdb_test "step" "inc .ecx.*" "step to inc ecx 1st time"
send_gdb "info reg ecx\n"
gdb_expect {
    -re "ecx *($hex)\t.*$gdb_prompt " {
	set preinc_ecx $expect_out(1,string)
    }
}

gdb_test "step" "inc .edx.*" "step to inc edx 1st time"
send_gdb "info reg edx\n"
gdb_expect {
    -re "edx *($hex)\t.*$gdb_prompt " {
	set preinc_edx $expect_out(1,string)
    }
}

gdb_test "step" "inc .ebx.*" "step to inc ebx 1st time"
send_gdb "info reg ebx\n"
gdb_expect {
    -re "ebx *($hex)\t.*$gdb_prompt " {
	set preinc_ebx $expect_out(1,string)
    }
}

gdb_test "step" "inc .esp.*" "step to inc esp 1st time"
send_gdb "info reg esp\n"
gdb_expect {
    -re "esp *($hex)\t.*$gdb_prompt " {
	set preinc_esp $expect_out(1,string)
    }
}

gdb_test "step" "inc .ebp.*" "step to inc ebp 1st time"
send_gdb "info reg ebp\n"
gdb_expect {
    -re "ebp *($hex)\t.*$gdb_prompt " {
	set preinc_ebp $expect_out(1,string)
    }
}

gdb_test "step" "inc .esi.*" "step to inc esi 1st time"
send_gdb "info reg esi\n"
gdb_expect {
    -re "esi *($hex)\t.*$gdb_prompt " {
	set preinc_esi $expect_out(1,string)
    }
}

gdb_test "step" "inc .edi.*" "step to inc edi 1st time"
send_gdb "info reg edi\n"
gdb_expect {
    -re "edi *($hex)\t.*$gdb_prompt " {
	set preinc_edi $expect_out(1,string)
    }
}

gdb_test "step" "dec .eax.*" "step to dec eax 1st time"
send_gdb "info reg eax\n"
gdb_expect {
    -re "eax *($hex)\t.*$gdb_prompt " {
	set predec_eax $expect_out(1,string)
    }
}

gdb_test "step" "dec .ecx.*" "step to dec ecx 1st time"
send_gdb "info reg ecx\n"
gdb_expect {
    -re "ecx *($hex)\t.*$gdb_prompt " {
	set predec_ecx $expect_out(1,string)
    }
}

gdb_test "step" "dec .edx.*" "step to dec edx 1st time"
send_gdb "info reg edx\n"
gdb_expect {
    -re "edx *($hex)\t.*$gdb_prompt " {
	set predec_edx $expect_out(1,string)
    }
}

gdb_test "step" "dec .ebx.*" "step to dec ebx 1st time"
send_gdb "info reg ebx\n"
gdb_expect {
    -re "ebx *($hex)\t.*$gdb_prompt " {
	set predec_ebx $expect_out(1,string)
    }
}

gdb_test "step" "dec .esp.*" "step to dec esp 1st time"
send_gdb "info reg esp\n"
gdb_expect {
    -re "esp *($hex)\t.*$gdb_prompt " {
	set predec_esp $expect_out(1,string)
    }
}

gdb_test "step" "dec .ebp.*" "step to dec ebp 1st time"
send_gdb "info reg ebp\n"
gdb_expect {
    -re "ebp *($hex)\t.*$gdb_prompt " {
	set predec_ebp $expect_out(1,string)
    }
}

gdb_test "step" "dec .esi.*" "step to dec esi 1st time"
send_gdb "info reg esi\n"
gdb_expect {
    -re "esi *($hex)\t.*$gdb_prompt " {
	set predec_esi $expect_out(1,string)
    }
}

gdb_test "step" "dec .edi.*" "step to dec edi 1st time"
send_gdb "info reg edi\n"
gdb_expect {
    -re "edi *($hex)\t.*$gdb_prompt " {
	set predec_edi $expect_out(1,string)
    }
}

# gdb_test "step" "end inc_dec_tests .*" "step to end inc_dec_tests 1st time"

gdb_test "break $end_of_main" \
    "Breakpoint $decimal at .* line $end_of_main\." \
    "set breakpoint at end of main"

gdb_test "continue" \
    " end of main .*" \
    "continue to end of main"

gdb_test "break $end_of_inc_dec_tests" \
    "Breakpoint $decimal at .* line $end_of_inc_dec_tests\." \
    "set breakpoint at end of inc_dec_tests"

gdb_test "reverse-continue" \
    " end inc_dec_tests .*" \
    "reverse to inc_dec_tests"

#
# Now reverse step, and check register values.
#

gdb_test "info reg edi" "edi *$preinc_edi\t.*" "edi before reverse-dec"
gdb_test "reverse-step" "dec .edi.*" "reverse-step to dec edi"
gdb_test "info reg edi" "edi *$predec_edi\t.*" "edi after reverse-dec"

gdb_test "info reg esi" "esi *$preinc_esi\t.*" "esi before reverse-dec"
gdb_test "reverse-step" "dec .esi.*" "reverse-step to dec esi"
gdb_test "info reg esi" "esi *$predec_esi\t.*" "esi after reverse-dec"

gdb_test "info reg ebp" "ebp *$preinc_ebp\t.*" "ebp before reverse-dec"
gdb_test "reverse-step" "dec .ebp.*" "reverse-step to dec ebp"
gdb_test "info reg ebp" "ebp *$predec_ebp\t.*" "ebp after reverse-dec"

gdb_test "info reg esp" "esp *$preinc_esp\t.*" "esp before reverse-dec"
gdb_test "reverse-step" "dec .esp.*" "reverse-step to dec esp"
gdb_test "info reg esp" "esp *$predec_esp\t.*" "esp after reverse-dec"

gdb_test "info reg ebx" "ebx *$preinc_ebx\t.*" "ebx before reverse-dec"
gdb_test "reverse-step" "dec .ebx.*" "reverse-step to dec ebx"
gdb_test "info reg ebx" "ebx *$predec_ebx\t.*" "ebx after reverse-dec"

gdb_test "info reg edx" "edx *$preinc_edx\t.*" "edx before reverse-dec"
gdb_test "reverse-step" "dec .edx.*" "reverse-step to dec edx"
gdb_test "info reg edx" "edx *$predec_edx\t.*" "edx after reverse-dec"

gdb_test "info reg ecx" "ecx *$preinc_ecx\t.*" "ecx before reverse-dec"
gdb_test "reverse-step" "dec .ecx.*" "reverse-step to dec ecx"
gdb_test "info reg ecx" "ecx *$predec_ecx\t.*" "ecx after reverse-dec"

gdb_test "info reg eax" "eax *$preinc_eax\t.*" "eax before reverse-dec"
gdb_test "reverse-step" "dec .eax.*" "reverse-step to dec eax"
gdb_test "info reg eax" "eax *$predec_eax\t.*" "eax after reverse-dec"

gdb_test "info reg edi" "edi *$predec_edi\t.*" "edi before reverse-inc"
gdb_test "reverse-step" "inc .edi.*" "reverse-step to inc edi"
gdb_test "info reg edi" "edi *$preinc_edi\t.*" "edi after reverse-inc"

gdb_test "info reg esi" "esi *$predec_esi\t.*" "esi before reverse-inc"
gdb_test "reverse-step" "inc .esi.*" "reverse-step to inc esi"
gdb_test "info reg esi" "esi *$preinc_esi\t.*" "esi after reverse-inc"

gdb_test "info reg ebp" "ebp *$predec_ebp\t.*" "ebp before reverse-inc"
gdb_test "reverse-step" "inc .ebp.*" "reverse-step to inc ebp"
gdb_test "info reg ebp" "ebp *$preinc_ebp\t.*" "ebp after reverse-inc"

gdb_test "info reg esp" "esp *$predec_esp\t.*" "esp before reverse-inc"
gdb_test "reverse-step" "inc .esp.*" "reverse-step to inc esp"
gdb_test "info reg esp" "esp *$preinc_esp\t.*" "esp after reverse-inc"

gdb_test "info reg ebx" "ebx *$predec_ebx\t.*" "ebx before reverse-inc"
gdb_test "reverse-step" "inc .ebx.*" "reverse-step to inc ebx"
gdb_test "info reg ebx" "ebx *$preinc_ebx\t.*" "ebx after reverse-inc"

gdb_test "info reg edx" "edx *$predec_edx\t.*" "edx before reverse-inc"
gdb_test "reverse-step" "inc .edx.*" "reverse-step to inc edx"
gdb_test "info reg edx" "edx *$preinc_edx\t.*" "edx after reverse-inc"

gdb_test "info reg ecx" "ecx *$predec_ecx\t.*" "ecx before reverse-inc"
gdb_test "reverse-step" "inc .ecx.*" "reverse-step to inc ecx"
gdb_test "info reg ecx" "ecx *$preinc_ecx\t.*" "ecx after reverse-inc"

gdb_test "info reg eax" "eax *$predec_eax\t.*" "eax before reverse-inc"
gdb_test "reverse-step" "inc .eax.*" "reverse-step to inc eax"
gdb_test "info reg eax" "eax *$preinc_eax\t.*" "eax after reverse-inc"


